package com.example.demo.start;

public class StudyThreadImplementsRunnable implements Runnable {

    private int total = 100;

    private int no = total+1;

    private final Object obj = new Object();

    @Override
    public void run() {
        while (true){
            synchronized (this.obj){
                if (this.total > 0 ){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String msg = Thread.currentThread().getName()+" 售出第   "+(this.no -this.total) +"  张票";
                    System.out.println(msg);
                    this.total--;
                }else {
                    System.out.println("票已售完，请下次再来！");
                    System.exit(0);
                }
            }
        }
    }
}
